package com.lrw.controller;

import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang.StringUtils;
import org.codehaus.jackson.JsonParseException;
import org.codehaus.jackson.map.JsonMappingException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import com.lrw.entity.UserModel;
import com.lrw.service.UserService;
import com.lrw.util.ComparatorUtil;
import com.vingsoft.iframework.orm.Page;
import com.vingsoft.iframework.orm.PropertyFilter;
import com.vingsoft.iframework.utils.ServletUtils;
import com.vingsoft.iframework.utils.SpringMVCUtils;

@Controller
@RequestMapping("/login/login.do")
public class HelloWorldController {
	@Autowired
	private UserService userService;

	/**
	 * 用户登陆验证
	 * 
	 * @param request
	 * @param response
	 * @return
	 */
	@RequestMapping(params = "method=login")
	public String login(HttpServletRequest request, HttpServletResponse response) {
		String username = request.getParameter("username");
		String password = request.getParameter("password");
		List<UserModel> list = userService.getLogin(username, password);
		if (list.size() > 0) {
			return "/login/crmleft";
		} else {
			return "/login/error";
		}
	}

	@RequestMapping(params = "method=queryList")
	public void queryList(HttpServletRequest request,
			HttpServletResponse response) {
		String username = request.getParameter("username");
		String password = request.getParameter("password");
		Page<UserModel> pages = new Page<UserModel>(10);
		List<PropertyFilter> filters = new ArrayList<PropertyFilter>();
		int pagee = Integer.valueOf(request.getParameter("page"));// 页号
		int rp = Integer.valueOf(request.getParameter("rp"));// 每页条数
		String sortname = request.getParameter("sortname"); // 排序字段
		String sortorder = request.getParameter("sortorder");// 排序方式
		if (!pages.isOrderBySetted()) {
			pages.setOrderBy(sortname);
			pages.setOrder(sortorder);
		}

		if (StringUtils.isNotBlank(username)) {
			PropertyFilter filter = new PropertyFilter("LIKES_username",
					username);
			filters.add(filter);
			filter = null;
		}
		pages.setPageNo(pagee);
		pages.setPageSize(rp);

		List<UserModel> list = userService.findAll(username, password);
		if (list != null && list.size() > 0)
			ComparatorUtil.Comparator(list, sortname, false);// 排序 sortname降序
		com.lrw.util.Page p = new com.lrw.util.Page(rp, pagee, list);// 调用分页信息
		Map map = new HashMap();
		map.put("rows", p.getPageList());
		map.put("total", p.getAllColumn());
		map.put("page", pagee);
		SpringMVCUtils.renderJson(response, map);
		map = null;
	}

	@RequestMapping(params = "method=input")
	public String input(ModelMap model, UserModel userModel,
			HttpServletRequest request) throws JsonParseException,
			JsonMappingException, IOException {
		String username = "sad";
		return "/login/user-input";
	}

	@SuppressWarnings( { "static-access", "deprecation" })
	@RequestMapping(params = "method=save")
	public void save(UserModel userModel, HttpServletRequest request,
			HttpServletResponse response) {
		userService.saveOrUpdate(userModel);
		userModel = null;
		SpringMVCUtils.render(response, ServletUtils.TEXT_TYPE, "true");
	}

	/**
	 * 新增中心
	 */
	@RequestMapping(params = "method=userAdd")
	public String userAdd() {
		return "/login/user-add";
	}

	/**
	 * 中心列表tab
	 */
	@RequestMapping(params = "method=listTab")
	// distictTab
	public String listTab(ModelMap model, HttpServletRequest request) {
		// model.addAttribute("parentCode", request.getParameter("parentCode"));
		return "/login/user-tab";
	}

	/**
	 * 中心列表页
	 */
	@RequestMapping(params = "method=list")
	// list
	public String list(ModelMap model, HttpServletRequest request) {
		// model.addAttribute("parentCode", request.getParameter("parentCode"));
		return "/login/hello";
	}

	/*
	 * 删除
	 */
	@RequestMapping(params = "method=delete")
	public void delete(@RequestParam UserModel id, HttpServletResponse response) {
		userService.delete(id);
		SpringMVCUtils.render(response, ServletUtils.TEXT_TYPE, "true");
	}
}